/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package units.villagers;

import map.World;

import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import units.NonFightingUnit;
import units.Player;
import units.Unit;

/** Represents the Villager characters
 * 
 */
public abstract class Villager extends NonFightingUnit
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Create a new Villager object
	 * @param x The x coordinate in of the villager
	 * @param y The y coordinate in of the villager
	 * @param maxHP The maximum HP of the villager
	 * @throws SlickException
	 */
    public Villager(World world, int team)
    throws SlickException
    {
    	
    	// temporarily make the super class and then replace the images
    	super(world, team);
    	this.setDialogueTimer(DIALOG_INTERVAL);
       	this.setKillable(false);
    	
    }
    
    @Override
    public void update(Input input, int delta)
    {
    	if (this.isAlive()) {
    		
    		Player player = this.getWorld().getPlayer();
    		
	    	double dist2 = calculateDistanceSqrd(player.getX(), player.getY(), this.getX(), this.getY());
	    	
	    	// makes sure the dialog is shown only for a limited time
	    	if (this.isShowUnitDialogue()) {
		     	this.setDialogueTimer(this.getDialogueTimer()-delta);
		     	if ((this.getDialogueTimer() <= 0) || (dist2 > Unit.Squared(VISIBLITY))) {
		     		this.setDialogueTimer(DIALOG_INTERVAL);
		     		this.hideDialogue();
		     	}
	    	}
	    	
	    	// make the villager look at the player when interacting with the player
	    	if (!player.isTeleporting() && player.isAlive() && (dist2 <= Unit.Squared(VISIBLITY))) {
	    		double distx = player.getX()-this.getX();
	    		if (player.getY() < this.getMinY()) { this.lookUp(); }
	    		else if (distx < 0) { this.lookLeft(); }
	    		else if (distx > 0 ) { this.lookRight(); }
	    	} else {
	    		this.idle(delta);
	    	}
	    	
	    	this.updateFromScript(input, delta);
	    	
    	} else {
    		
    		this.checkDieing(false, 0, 0);
    		
    	}
    	
    	super.update(input, delta);
    	
    }
    
    /* Static Methods */
    
    public static boolean isHealer(Villager v)
    {
    	return (v instanceof Healer);
    }
    
    public static boolean isGuider(Villager v)
    {
    	return (v instanceof Guider);
    }
    
    public static boolean isIdler(Villager v)
    {
    	return (v instanceof Idler);
    }
    
    /* Getter and Setters beyond this point. */
  
}
